#!/bin/sh
#
# $Id: compiler.sh.src,v 1.3 2006/02/02 14:55:07 jurgenv Exp $
#
# compile.sh (C) 1999, Merijn de Jonge (mdejonge@cwi.nl)
#
# Compiler to which translates Pico programs to stack machine programs.
#
# The compiler reads a Pico program on standard input and writes on standard
# output the corresponding stack machine program.
# On error (in case of syntax error) the term
#   syntax-error(line,col) 
# is written to standard output indicating the location of the error.
#

TOP=__TOP__
META=__META__

# Programs
ASOURCE="$META/bin/unparsePT"
SGLR="$META/bin/sglr"
POSINFO="$META/bin/addPosInfo"
TRMCAT="$META/bin/trmcat"
APPLY_FUNC="$META/bin/apply-function"

LD_LIBRARY_PATH=$META/lib:$LD_LIBRARY_PATH
export LD_LIBRARY_PATH

# Location of several files (possible needs manual configuration)
COMPILER="$TOP/compiler/src/Pico-tr"
PARSETBL="$TOP/compiler/src/Pico-tr.trm.tbl"


# Temporary files, whihc are removed on termination
tmp=/tmp/`basename $0`.$$
trap "rm -f $tmp.*" 0


# Handle parse error. Obtain error details form error file.
# Construct and print the term syntax-error(line,col)
#
parse_error ()
{
   line=`cat ${tmp}.parser.out | sed 's/.*area(//;s/,.*).*//'`
   col=`cat ${tmp}.parser.out | sed 's/.*area([0-9]*,//;s/,.*).*//'`
   echo "syntax-error($line,$col)" >&1
   exit 1
}


# Send input to parser. We embed the Pico program within  "tr" [ ... ]
# to construct an input term for the real compiler.
(cat) | ${SGLR} -t -o ${tmp}.parser.out -p ${PARSETBL} 2>/dev/null || parse_error

${POSINFO} -p "/dev/null" -i ${tmp}.parser.out | ${APPLY_FUNC} -f 'tr' -s 'I-LIST' | ${COMPILER} 2>/dev/null | ${ASOURCE} | ${TRMCAT} > ${tmp}.compiler-output

cat ${tmp}.compiler-output 

exit 0
